"""
缺陷相关的数据传输对象(DTO)定义
用于 API 请求和响应的数据模型
"""
from datetime import datetime
from typing import Optional, List, Dict, Any
from pydantic import BaseModel, Field
from app.models.base import PyObjectId
from app.models.defect import (
    DefectPriority,
    DefectSeverity,
    DefectType,
    DefectSource,
    DefectEnvironmentInfo,
)

# ========== 评论相关 Schema ==========
class DefectCommentCreate(BaseModel):
    """创建缺陷评论模型"""
    content: str = Field(..., min_length=1, max_length=2000, description="评论内容")
    parent_comment_id: Optional[str] = Field(None, description="父评论ID，用于回复功能")

class DefectCommentUpdate(BaseModel):
    """更新缺陷评论模型"""
    content: Optional[str] = Field(None, min_length=1, max_length=2000, description="评论内容")
    is_deleted: Optional[bool] = Field(None, description="是否已删除，用于撤销评论")

class DefectCommentResponse(BaseModel):
    """缺陷评论响应模型"""
    id: str = Field(..., description="评论ID")
    content: str = Field(..., description="评论内容")
    author_id: str = Field(..., description="作者ID")
    parent_comment_id: Optional[str] = Field(None, description="父评论ID")
    is_deleted: bool = Field(default=False, description="是否已删除")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: Optional[datetime] = Field(None, description="更新时间")

# ========== 附件相关 Schema ==========
class DefectAttachmentCreate(BaseModel):
    """创建缺陷附件模型"""
    filename: str = Field(..., description="文件名")
    file_path: str = Field(..., description="文件路径")
    file_size: int = Field(..., description="文件大小")
    content_type: str = Field(..., description="文件类型")

class DefectAttachmentResponse(BaseModel):
    """缺陷附件响应模型"""
    id: str = Field(..., description="附件ID")
    filename: str = Field(..., description="文件名")
    file_path: str = Field(..., description="文件路径")
    file_size: int = Field(..., description="文件大小")
    content_type: str = Field(..., description="文件类型")
    uploaded_by: str = Field(..., description="上传者ID")
    uploaded_at: datetime = Field(..., description="上传时间")

# ========== 创建和更新 Schema ==========
class DefectCreate(BaseModel):
    """创建缺陷模型"""
    title: str = Field(..., min_length=1, max_length=200, description="缺陷标题")
    description: Optional[str] = Field(None, description="缺陷描述")
    type: DefectType = Field(..., description="缺陷类型")
    status: str = Field(default="", description="缺陷状态（从状态管理系统动态获取，创建时使用默认状态）")
    priority: DefectPriority = Field(DefectPriority.MEDIUM, description="优先级")
    severity: DefectSeverity = Field(DefectSeverity.MINOR, description="严重程度")
    source: DefectSource = Field(..., description="缺陷来源")
    project_id: Optional[PyObjectId] = Field(None, description="关联项目ID")
    assignee_id: Optional[PyObjectId] = Field(None, description="负责人ID")
    reporter_id: Optional[PyObjectId] = Field(..., description="报告人ID")
    verifier_id: Optional[PyObjectId] = Field(None, description="验证人ID")
    steps_to_reproduce: Optional[str] = Field(None, description="复现步骤")
    expected_result: Optional[str] = Field(None, description="预期结果")
    actual_result: Optional[str] = Field(None, description="实际结果")
    environment_info: Optional[DefectEnvironmentInfo] = Field(None, description="环境信息")
    found_date: Optional[datetime] = Field(None, description="发现时间")
    completed_at: Optional[datetime] = Field(None, description="完成时间")
    actual_completed_at: Optional[datetime] = Field(None, description="实际完成时间")
    tags: List[str] = Field(default_factory=list, description="标签")
    organization_id: Optional[str] = Field(None, description="组织ID")

class DefectUpdate(BaseModel):
    """更新缺陷模型"""
    title: Optional[str] = Field(None, min_length=1, max_length=200, description="缺陷标题")
    description: Optional[str] = Field(None, description="缺陷描述")
    type: Optional[DefectType] = Field(None, description="缺陷类型")
    status: Optional[str] = Field(None, description="缺陷状态")
    priority: Optional[DefectPriority] = Field(None, description="优先级")
    severity: Optional[DefectSeverity] = Field(None, description="严重程度")
    source: Optional[DefectSource] = Field(None, description="缺陷来源")
    project_id: Optional[PyObjectId] = Field(None, description="关联项目ID")
    assignee_id: Optional[PyObjectId] = Field(None, description="负责人ID")
    reporter_id: Optional[PyObjectId] = Field(None, description="报告人ID")
    verifier_id: Optional[PyObjectId] = Field(None, description="验证人ID")
    version_id: Optional[str] = Field(None, description="关联版本ID（通过关联表）")
    steps_to_reproduce: Optional[str] = Field(None, description="复现步骤")
    expected_result: Optional[str] = Field(None, description="预期结果")
    actual_result: Optional[str] = Field(None, description="实际结果")
    environment_info: Optional[DefectEnvironmentInfo] = Field(None, description="环境信息")
    found_date: Optional[datetime] = Field(None, description="发现时间")
    resolved_date: Optional[datetime] = Field(None, description="解决时间")
    verified_date: Optional[datetime] = Field(None, description="验证时间")
    completed_at: Optional[datetime] = Field(None, description="完成时间")
    actual_completed_at: Optional[datetime] = Field(None, description="实际完成时间")
    tags: Optional[List[str]] = Field(None, description="标签")

# ========== 操作相关 Schema ==========
class DefectStatusUpdate(BaseModel):
    """缺陷状态更新模型"""
    status: str = Field(..., description="新状态")

class DefectAssigneeUpdate(BaseModel):
    """缺陷负责人更新模型"""
    assignee_id: Optional[str] = Field(None, description="新负责人ID")

# ========== 响应模型 Schema ==========
class DefectResponse(BaseModel):
    """缺陷响应模型"""
    id: str = Field(..., description="缺陷ID")
    title: str = Field(..., description="缺陷标题")
    description: Optional[str] = Field(None, description="缺陷描述")
    type: DefectType = Field(..., description="缺陷类型")
    status: str = Field(..., description="缺陷状态")
    priority: DefectPriority = Field(..., description="优先级")
    severity: DefectSeverity = Field(..., description="严重程度")
    source: DefectSource = Field(..., description="缺陷来源")
    project_id: Optional[str] = Field(None, description="关联项目ID")
    assignee_id: Optional[str] = Field(None, description="负责人ID")
    reporter_id: Optional[str] = Field(None, description="报告人ID")
    verifier_id: Optional[str] = Field(None, description="验证人ID")
    steps_to_reproduce: Optional[str] = Field(None, description="复现步骤")
    expected_result: Optional[str] = Field(None, description="预期结果")
    actual_result: Optional[str] = Field(None, description="实际结果")
    environment_info: Optional[DefectEnvironmentInfo] = Field(None, description="环境信息")
    found_date: Optional[datetime] = Field(None, description="发现时间")
    resolved_date: Optional[datetime] = Field(None, description="解决时间")
    verified_date: Optional[datetime] = Field(None, description="验证时间")
    completed_at: Optional[datetime] = Field(None, description="完成时间")
    actual_completed_at: Optional[datetime] = Field(None, description="实际完成时间")
    tags: List[str] = Field(default_factory=list, description="标签")
    comments: List[DefectCommentResponse] = Field(default_factory=list, description="评论列表")
    attachments: List[DefectAttachmentResponse] = Field(default_factory=list, description="附件列表")
    created_by: str = Field(..., description="创建者ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    comment_count: int = Field(0, description="评论数量")
    attachment_count: int = Field(0, description="附件数量")

class DefectSummary(BaseModel):
    """缺陷摘要模型"""
    id: str = Field(..., description="缺陷ID")
    title: str = Field(..., description="缺陷标题")
    type: DefectType = Field(..., description="缺陷类型")
    status: str = Field(..., description="缺陷状态")
    priority: DefectPriority = Field(..., description="优先级")
    severity: DefectSeverity = Field(..., description="严重程度")
    assignee_id: Optional[str] = Field(None, description="负责人ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

# ========== 筛选和统计 Schema ==========
class DefectFilter(BaseModel):
    """缺陷筛选模型"""
    search: Optional[str] = Field(None, description="搜索关键词")
    project_id: Optional[str] = Field(None, description="项目ID")
    assignee_id: Optional[str] = Field(None, description="负责人ID")
    reporter_id: Optional[str] = Field(None, description="报告人ID")
    status: Optional[str] = Field(None, description="状态")
    priority: Optional[DefectPriority] = Field(None, description="优先级")
    severity: Optional[DefectSeverity] = Field(None, description="严重程度")
    defect_type: Optional[DefectType] = Field(None, description="缺陷类型")
    source: Optional[DefectSource] = Field(None, description="缺陷来源")
    found_date_start: Optional[datetime] = Field(None, description="发现日期开始")
    found_date_end: Optional[datetime] = Field(None, description="发现日期结束")
    tags: Optional[List[str]] = Field(None, description="标签")

class DefectStats(BaseModel):
    """缺陷统计模型"""
    total_defects: int = Field(0, description="缺陷总数")
    defects_by_status: Dict[str, int] = Field(default_factory=dict, description="按状态分组的缺陷数")
    defects_by_priority: Dict[str, int] = Field(default_factory=dict, description="按优先级分组的缺陷数")
    defects_by_severity: Dict[str, int] = Field(default_factory=dict, description="按严重程度分组的缺陷数")
    defects_by_type: Dict[str, int] = Field(default_factory=dict, description="按类型分组的缺陷数")
    defects_by_source: Dict[str, int] = Field(default_factory=dict, description="按来源分组的缺陷数")

# ========== 活动日志 Schema ==========
class DefectActivity(BaseModel):
    """缺陷活动记录模型"""
    id: str = Field(..., description="活动ID")
    defect_id: str = Field(..., description="缺陷ID")
    user_id: str = Field(..., description="操作用户ID")
    action: str = Field(..., description="操作类型")
    description: str = Field(..., description="操作描述")
    old_value: Optional[str] = Field(None, description="旧值")
    new_value: Optional[str] = Field(None, description="新值")
    created_at: datetime = Field(..., description="操作时间")

